//
// Created by HZH on 2023-05-02.
//

#include <string.h>
#include <stdio.h>
#include "BzDB_BzMap_t.h"


//typedef struct BzDB_BzMap_t{
//
//    void * name_list;
//    int name_size;
//
//    void * buf;
//    void * end;
//    int buffer_size;
//
//    int max_len;
//    int used_count;
//}BzDB_BzMap_t;

void BzDB_BzMap_init(BzDB_BzMap_t *bzMap,void * buf, int buffer_size , void * name_list , int name_size,int max_len)
{

    bzMap->buf = buf;
    bzMap->buffer_size = buffer_size;

    bzMap->name_list = name_list;
    bzMap->name_size = name_size;

    bzMap->max_len = max_len;

    bzMap->used_count = 0 ;

}

/**
*
 * set
 * get
 * delete
 *
*/
/**
 * // 如果相等就返回i
 * // 如果不相等就返回-1
 * @param bzMap
 * @param name
 * @return
 */
int BzDB_BzMap_find_name(BzDB_BzMap_t *bzMap , const char * name){
    void* pName = bzMap->name_list;


    for(int i = 0 ; i < bzMap->used_count ; i++){
        // 字符串的起始位置
        void * temp_start = bzMap->name_list + i * bzMap->name_size;
        const char * start_str = (const char * )temp_start;

        if(strcmp(name,start_str)==0){
            return i; // 如果相等就返回i
        }
    }
    return -1;// 如果不相等就返回-1
}

void BzDB_BzMap_set(BzDB_BzMap_t *bzMap , const char* name, void * data){
    // 如果超过最大数量，就不允许继续添加
    if(bzMap->used_count >= bzMap->max_len){
        return;
    }

    int index_ = BzDB_BzMap_find_name(bzMap , name);

    void * start_name_address = bzMap->name_list;
    void * start_data_address = bzMap->buf;

    if(index_ == -1){//没找到，就在最末尾加入
        start_data_address = start_data_address + bzMap->used_count * bzMap->buffer_size;
        start_name_address = start_name_address + bzMap->used_count * bzMap->name_size;

        strcpy(start_name_address,name);
        memcpy(start_data_address,data,bzMap->buffer_size);

        bzMap->used_count = bzMap->used_count + 1;
    } else{// 找到了就直接加入
        start_data_address = start_data_address + index_ * bzMap->buffer_size;
        start_name_address = start_name_address + index_ * bzMap->name_size;

        strcpy(start_name_address,name);
        memcpy(start_data_address,data,bzMap->buffer_size);
    }
}


CCBOOL BzDB_BzMap_get(BzDB_BzMap_t *bzMap , const char* name, void * data){
    int index_ = BzDB_BzMap_find_name(bzMap , name);

    if(index_ == -1){
        return FALSE;
    }

    void* start_data_address = bzMap->buf         +  index_ * bzMap->buffer_size;
    void * start_name_address = bzMap->name_list  +  index_ * bzMap->name_size;
    memcpy(data,start_data_address,bzMap->buffer_size);
    return TRUE;

}


CCBOOL BzDB_BzMap_delete(BzDB_BzMap_t *bzMap , const char* name , void * res)
{
    // 查找name是否存在
    int index = BzDB_BzMap_find_name(bzMap,name);

    if(index == -1){
        return FALSE;
    }

    void* item_address = bzMap->buf + bzMap->buffer_size*index;
    memcpy(res,item_address,bzMap->buffer_size);

    // 从后往前覆盖
    // index <- index+1
    // used_count-2 <- used_count-1
    for(int i = index ; i <=bzMap->used_count-2 ; i++)
    {
        // 删除data
        void * p1 = bzMap->buf +    i  * bzMap->buffer_size;
        void * p2 = bzMap->buf + (i+1) * bzMap->buffer_size;
        memcpy( p1 , p2 , bzMap->buffer_size  );

        // 删除name
        void * p_name1 = bzMap->name_list + i     * bzMap->name_size;
        void * p_name2 = bzMap->name_list + (i+1) * bzMap->name_size;
        memcpy( p_name1 , p_name2 , bzMap->name_size  );

    }
    bzMap->used_count = bzMap->used_count - 1;
    return TRUE;

}

void BzDB_BzMap_demo()
{
    typedef struct Person_t{
        char name[20];
        int age;
    }Person_t;

    BzDB_BzMap_t map;
    Person_t person_list[20];
    char name_list[20*30]; //20行，每行最多30个字符

    BzDB_BzMap_init(&map,person_list,sizeof(Person_t),name_list,30,20 );

    Person_t zhangsan;
    strcpy(zhangsan.name,"zs");
    zhangsan.age = 20;
    BzDB_BzMap_set(&map,"zhangsan",&zhangsan);


    Person_t lisi;
    strcpy(lisi.name,"ls");
    lisi.age = 30;
    BzDB_BzMap_set(&map,"lisi",&lisi);



    Person_t res;
    BzDB_BzMap_get(&map,"lisi",&res);

    printf("%s,%d,\n",res.name,res.age);

    Person_t delete_res;
    BzDB_BzMap_delete(&map,"zhangsan",&delete_res);

    int is_find = BzDB_BzMap_find_name(&map,"lisi");
    printf("%d\n",is_find);
}